<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline"
            v-if="checkPermission(['collectingAccount:add'])" @click="handleAdd">添加</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入支付方式名称" @advancedSearch="advancedSearchEvent" advanced>
            <template slot="advanced-content">
              <el-form ref="advancedForm" :model="advancedForm" label-width="90px">
                <el-form-item label="编号">
                  <el-input v-model="advancedForm.sn" placeholder="请输入编号" clearable></el-input>
                </el-form-item>
                <el-form-item label="状态">
                  <el-select v-model="advancedForm.enable_flag">
                    <el-option :key="0" label="启用" :value="true" />
                    <el-option :key="-1" label="禁用" :value="false" />
                  </el-select>
                </el-form-item>
                <el-form-item label="是否默认">
                  <el-select v-model="advancedForm.default_flag">
                    <el-option :key="0" label="是" :value="true" />
                    <el-option :key="-1" label="否" :value="false" />
                  </el-select>
                </el-form-item>
              </el-form>
            </template>
          </en-table-search>
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="sn" label="编号" />
        <el-table-column prop="default_flag" label="是否默认" >
          <template slot-scope="scope">
            {{ scope.row.default_flag ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="是否启用" key="enable_flag">
          <template slot-scope="scope">
            <el-switch v-if="checkPermission(['collectingAccount:updateEnableFlag'])"
              v-model="scope.row.enable_flag"
              :active-value="true"
              :inactive-value="false"
              @change="handleUpdateEnableFlag(scope.row)"
            />
            <span v-else>{{ scope.row.enable_flag ? '启用' : '禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" v-if="checkPermission(['collectingAccount:edit'])"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="primary" v-if="checkPermission(['collectingAccount:updateDefault'])"
                       @click="handleUpdateDefault(scope.row.id)">设为默认</el-button>
            <el-button size="mini" type="danger"
              v-if="checkPermission(['collectingAccount:delete'])"
              @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
    <el-dialog :title="collectingAccountForm.id ? '编辑支付方式' : '添加支付方式'" :visible.sync="dialogVisible" width="500px"
      @open="handleDialogOpen" :modal-append-to-body="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="collectingAccountForm" :rules="formRules" ref="collectingAccountForm" label-width="80px">
        <el-form-item label="编号" prop="sn" v-if="collectingAccountForm.id">
          <el-input v-model="collectingAccountForm.sn" disabled ></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="collectingAccountForm.name" :maxlength="20" clearable placeholder="请输入支付方式名称"></el-input>
        </el-form-item>
        <el-form-item label="是否默认" prop="default_flag">
          <el-radio-group v-model="collectingAccountForm.default_flag">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="enable_flag">
          <el-radio-group v-model="collectingAccountForm.enable_flag">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="collectingAccountForm.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 4 }"
                    placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCollectingAccountForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_CollectingAccount from '@/api/collectingAccount'
export default {
  name: 'collectingAccount',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 添加/编辑表单
      collectingAccountForm: {},
      advancedForm: {

      },
      // 添加|编辑 表单规则
      formRules: {
        name: [
          this.MixinRequired('支付方式名称必填！'),
          { min: 2, max: 30, message: '长度在 2 到 30 个字符' }
        ]
      },
      // 是否显示 添加|编辑 表单
      dialogVisible: false
    }
  },
  watch: {
  },
  mounted() {
    this.GET_CollectingAccountList()
  },
  methods: {
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      this.params.page_no = 1
      this.GET_CollectingAccountList()
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_CollectingAccountList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_CollectingAccountList()
    },

    /** 添加弹窗 */
    handleAdd() {
      this.collectingAccountForm = {
        default_flag: true,
        enable_flag: true
      }
      this.dialogVisible = true
    },

    /** 编辑弹窗 */
    handleEdit(row) {
      this.collectingAccountForm = this.MixinClone(row)
      this.dialogVisible = true
    },
    /** 设为默认 */
    async handleUpdateDefault(id) {
      // 二次确认
      await this.$confirm(`确认要设置为默认支付方式吗?`, '提示')
      // 发起修改
      await API_CollectingAccount.updateDefault(id)
      this.$message.success('操作成功！')
      // 刷新列表
      await this.GET_CollectingAccountList()
    },
    /** 删除 */
    handleDelete(id) {
      this.$confirm('确定要删除吗？', '提示', { type: 'warning' }).then(() => {
        API_CollectingAccount.del(id).then(() => {
          this.$message.success('删除成功！')
          this.GET_CollectingAccountList()
        })
      }).catch(() => { })
    },
    /** 禁用/启用 */
    async handleUpdateEnableFlag(row) {
      try {
        // 二次确认
        const text = row.enable_flag ? '启用' : '禁用'
        await this.$confirm(`确认要${text}吗?`, '提示')
        // 发起修改
        await API_CollectingAccount.updateEnableFlag(row.id, row.enable_flag)
        this.$message.success('操作成功！')
        // 刷新列表
        await this.GET_CollectingAccountList()
      } catch (e) {
        // 取消或更新失败，恢复按钮
        row.enable_flag = !row.enable_flag
      }
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['collectingAccountForm'].clearValidate() })
    },

    /** 提交 添加|编辑 表单 */
    submitCollectingAccountForm() {
      this.$refs['collectingAccountForm'].validate((valid) => {
        if (valid) {
          const { id } = this.collectingAccountForm
          const data = this.MixinClone(this.collectingAccountForm)
          if (id) {
            API_CollectingAccount.edit(id, data).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_CollectingAccountList()
            })
          } else {
            API_CollectingAccount.add(data).then(() => {
              this.dialogVisible = false
              this.$message.success('添加成功！')
              this.GET_CollectingAccountList()
            })
          }
        } else {
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        name: data
      }
      this.params.page_no = 1
      this.GET_CollectingAccountList()
    },

    /** 查询分页列表 */
    GET_CollectingAccountList() {
      this.loading = true
      API_CollectingAccount.getList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

.birthday{
  width: 94%;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
